package bookManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BookDao {
    public void add(Book book) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();

            String sql = "insert into book values(null,?,?,?,?,0)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, book.getName());
            statement.setString(2, book.getAuthor());
            statement.setInt(3, book.getPrice());
            statement.setString(4, book.getType());

            int ret = statement.executeUpdate();
            if (ret != 1) {
                System.out.println("新增书籍失败！");
            } else {
                System.out.println("新增书籍成功！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, null);
        }
    }

    public List<Book> findBooks() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Book> books = new ArrayList<>();

        try {
            connection = DBUtil.getConnection();

            String sql = "select * from book";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Book book = new Book();
                book.setBookId(resultSet.getInt("bookId"));
                book.setName(resultSet.getString("name"));
                book.setAuthor(resultSet.getString("author"));
                book.setPrice(resultSet.getInt("price"));
                book.setType(resultSet.getString("type"));
                book.setBorrowed(resultSet.getInt("isBorrowed") == 1 ? true : false);
                books.add(book);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return books;
    }

    public List<Book> findBooksByName(String name) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Book> books = new ArrayList<>();
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from book where name = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, name);

            resultSet = statement.executeQuery();

            while (resultSet.next()) {
                Book book = new Book();
                book.setBookId(resultSet.getInt("bookId"));
                book.setName(resultSet.getString("name"));
                book.setAuthor(resultSet.getString("author"));
                book.setPrice(resultSet.getInt("price"));
                book.setType(resultSet.getString("type"));
                book.setBorrowed(resultSet.getInt("isBorrowed") == 1);
                books.add(book);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return books;
    }

    public boolean delete(int bookId) {
        Connection connection = null;
        PreparedStatement statement = null;

        try {
            connection = DBUtil.getConnection();
            String sql = "delete from book where bookId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, bookId);

            int ret = statement.executeUpdate();
            if (ret != 1) {
                return false;
            }
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, null);
        }
        return false;
    }

    public boolean borrowBook(int bookId) {
        Connection connection = null;
        PreparedStatement statement = null;
        PreparedStatement statement2 = null;
        ResultSet resultSet = null;

        try {
            connection = DBUtil.getConnection();
            String sql = "select * from book where bookId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, bookId);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                boolean isBorrowed = (resultSet.getInt("isBorrowed") == 1);
                if (isBorrowed) {
                    System.out.println("书已经被借出了！bookId= " + bookId);
                    return false;
                }
            } else {
                System.out.println("书不存在！ bookId= " + bookId);
                return false;
            }
            sql = "update book set isBorrowed = 1 where bookId = ?";
            statement2 = connection.prepareStatement(sql);
            statement2.setInt(1, bookId);
            int ret = statement2.executeUpdate();
            if (ret != 1) {
                System.out.println("修改借出状态失败！ bookId= " + bookId);
                return false;
            }
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //DBUtil.close(connection,statement,resultSet);
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement2 != null) {
                try {
                    statement2.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        return false;
    }

    public boolean returnBook(int bookId) {
        Connection connection = null;
        PreparedStatement statement = null;
        PreparedStatement statement2 = null;
        ResultSet resultSet = null;

        try {
            connection = DBUtil.getConnection();
            String sql = "select * from book where bookId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, bookId);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                boolean isBorrowed = (resultSet.getInt("isBorrowed") == 1);
                if (!isBorrowed) {
                    System.out.println("书已经被归还了！bookId= " + bookId);
                    return false;
                }
            } else {
                System.out.println("书不存在！ bookId= " + bookId);
                return false;
            }
            sql = "update book set isBorrowed = 0 where bookId = ?";
            statement2 = connection.prepareStatement(sql);
            statement2.setInt(1, bookId);
            int ret = statement2.executeUpdate();
            if (ret != 1) {
                System.out.println("修改借出状态失败！ bookId= " + bookId);
                return false;
            }
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //DBUtil.close(connection,statement,resultSet);
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement2 != null) {
                try {
                    statement2.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        return false;
    }

//    public static void main(String[] args) {
////        BookDao bookDao = new BookDao();
////        Book book = new Book();
////        book.setName("tanlei");
////        book.setAuthor("谭磊");
////        book.setType("gudianxiaoshuo");
////        book.setPrice(1000);
////        bookDao.add(book);
//
//        BookDao bookDao = new BookDao();
////        List<Book> books = bookDao.findBooks();
////        System.out.println(books);
//
////        List<Book> books = bookDao.findBooksByName("西游记");
////        System.out.println(books);
////
////        boolean ret = bookDao.delete(4);
////        System.out.println(ret);
//
//        boolean ret = bookDao.borrowBook(1);
//        System.out.println(ret);
//    }
}
